//
//  OMCEasyARViewController.m
//  OMCEasyARApp
//
//  Created by Liu Guocheng on 2018/4/20.
//  Copyright © 2018年 Sumavision. All rights reserved.
//

#import "OMCEasyARViewController.h"
#import "OpenGLView.h"

@interface OMCEasyARViewController ()

@property (nonatomic, strong) UIButton *backButton;

@end

@implementation OMCEasyARViewController
{
    OpenGLView *glView;
}

- (void)loadView
{
    [super loadView];
    self->glView = [[OpenGLView alloc] initWithFrame:CGRectZero];
    self.view = self->glView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.backButton];
    [self->glView setOrientation:self.interfaceOrientation];
    [self->glView start];

}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self->glView stop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillLayoutSubviews
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets insets = self.view.safeAreaInsets;
        contentInsets = insets;
    } else {
        id <UILayoutSupport>topGuide = self.topLayoutGuide;
        id <UILayoutSupport>bottomGuide = self.topLayoutGuide;
        contentInsets = UIEdgeInsetsMake(topGuide.length, 0, bottomGuide.length, 0);
    }
    
    {
        CGFloat sWidth = 28;
        CGFloat sMarginX = 16;
        CGFloat sMarginY = contentInsets.top + 16;
        self.backButton.frame = CGRectMake(sMarginX, sMarginY, sWidth, sWidth);
    }
    
    [self->glView resize:self.view.bounds orientation:self.interfaceOrientation];
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self->glView setOrientation:toInterfaceOrientation];
}

- (UIButton *)backButton
{
    if (_backButton == nil) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"player_back_lsmallwindow"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (void)backAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
